﻿//Write a program to calculate the sum (with accuracy of 0.001): 1 + 1/2 - 1/3 + 1/4 - 1/5 + ...

using System;

class CalculateSumWIthAccuracy
{
    static void Main()
    {
        decimal oldSum = 1M;
        decimal newSum = 1M;
        decimal denominator = 2M;
        decimal accuracy;
        do
        {
            oldSum = newSum;
            if (denominator % 2 == 0)
                newSum = newSum + ((decimal)1 / denominator);
            else
                newSum = newSum - ((decimal)1 / denominator);
            denominator++;
            accuracy = (decimal)Math.Abs(newSum - oldSum);
        } while (accuracy >= 0.001M);
        Console.WriteLine("1 + 1/2 - 1/3 + 1/4 - 1/5 + .... 1/" + denominator + " = {0}", oldSum);
    }
}
